export default {
    install: (app: { directive: (arg0: string, arg1: { mounted(el: any, binding: any): void; beforeUnmount(el: any, binding: any): void; }) => void; }) => {
        app.directive('debounce', {
            mounted(el, binding) {
                if (typeof binding.value.fn !== 'function' || !binding.value.event) {
                    return
                }
                let delay = 2000;
                el.timer = null;
                el.handler = function () {
                    if (el.timer) {
                        clearTimeout(el.timer)
                        el.timer = null
                    }
                    el.timer = setTimeout(() => {
                        console.log(this, arguments);
                        binding.value.fn.apply(this, arguments)
                        el.timer = null
                    }, binding.value.delay || delay)
                }
                el.addEventListener(binding.value.event, el.handler)

            },
            beforeUnmount(el, binding) {
                if (el.timer) {
                    clearTimeout(el.timer)
                    el.timer = null
                }
                el.removeEventListener(binding.value.evnet, el.handler)
            },
        })
    }
}